from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.status import HTTP_404_NOT_FOUND, HTTP_204_NO_CONTENT

from .models import Student, Classes
from .serializers import StudentSerializer, ClassesSerializer


# Create your views here.
class StudentViewSet(viewsets.ModelViewSet):
    # 返回所有的学生数据
    # def list(self, request):
    #     # 返回的是模型类集合
    #     students = Student.objects.all()
    #     # 现在要转json  即序列化，传instance
    #     # 实例化序列化器，传入要转换的模型集合，如果是多条数据，一般我们加上many = True
    #     serializer = StudentSerializer(students, many=True)
    #     # 返回响应： 序列化后的数据
    #     # 查看源码可知Response中第一个参数就是data，所以data可以省略
    #     return Response(data=serializer.data)
    #
    # def create(self, request):
    #     # 1.获取前端传入的数据,前端传入的是json数据 post请求直接用.data获取数据
    #     # data = request.data  # 得到了前端的json数据
    #     # ModelSerializer的第一个参数时instance,所以data = 不能省略
    #     serializer = StudentSerializer(data=request.data)  # json转模型，称为反序列化,传入data
    #     # 2.校验,转成模型后进行校验
    #     serializer.is_valid(raise_exception=True)  # 根据写的校验逻辑进行校验，不通过就抛出异常
    #     # 3.保存数据
    #     serializer.save()  # 看save源码可知当instance属性为空时做create操作，不为空是做update操作，此处没有给instance
    #     # 4.返回响应: 返回序列化数据
    #     return Response(serializer.data)
    #
    # # 查单一数据
    # def retrieve(self, request, pk):
    #     try:
    #         student = Student.objects.get(id=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=HTTP_404_NOT_FOUND)
    #
    #     serializer = StudentSerializer(instance=student)  # 序列化 instance可省略
    #     return Response(serializer.data)
    #
    # def update(self, request, pk):
    #     # 查询要修改的数据
    #     try:
    #         student = Student.objects.get(id=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=HTTP_404_NOT_FOUND)
    #
    #     # 获取前端传入的数据进行反序列化
    #     serializer = StudentSerializer(instance=student, data=request.data)
    #     # 校验
    #     serializer.is_valid(raise_exception=True)
    #     # 对数据进行修改
    #     serializer.save()
    #     return Response(serializer.data)  # 通过序列化器对象的data属性获取序列化后的数据 （json格式的数据）
    #
    # def destroy(self,request,pk):
    #     try:
    #         student = Student.objects.get(id=pk)
    #     except Student.DoesNotExist:
    #         return Response(status=HTTP_404_NOT_FOUND)
    #     student.delete()
    #     return Response(status=HTTP_204_NO_CONTENT)

    """
    list:

    返回学生列表数据
    """

    # 指定查询集
    queryset = Student.objects.filter(is_delete=False, classes__is_delete=False)
    # 指定序列化器
    serializer_class = StudentSerializer


class ClassesViewSet(viewsets.ModelViewSet):
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassesSerializer

    #drf提供了一个装饰器来添加路由
    @action(methods=['get'],detail=False)  # 不需要返回详情,不返回id
    def last(self, request):
        classes = Classes.objects.filter(is_delete=False).last()
        # get_serializer拿到指定的序列化器，拿到classes的序列化器
        serializer = self.get_serializer(classes)
        return Response(serializer.data)
